
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.tompai.common.lock;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
* @desc: tompai-common
* @name: NormalReadThread.java
* @author: tompai
* @email：liinux@qq.com
* @createTime: 2021年2月7日 下午11:48:56
* @history:
* @version: v1.0
*/

public class NormalReadThread extends Thread{

    private String sourceFile;
    private String threadName;

    public NormalReadThread(String sourceFile,String threadName){
        this.sourceFile = sourceFile;
        this.threadName = threadName;
    }

    @Override
    public void run(){
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(sourceFile, "rw");
            FileChannel fc = raf.getChannel();
            StringBuffer sb = new StringBuffer();
            sb.append("Read from "+this.threadName+"：");
            ByteBuffer bb = ByteBuffer.allocate(1024);
            while((fc.read(bb))!=-1){
                //sb.append(new String(bb.array()));
                bb.clear();
            }
            System.out.println(this.threadName+" : read success!");
            raf.close();
            fc.close();
        } catch(Exception e){
            e.printStackTrace();
            System.out.println(this.threadName+" : read failed!!");
        }
    }

}

